﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using GameEngine.Clients.Silverlight.View.Components;

namespace GameEngine.Clients.Silverlight.View
{
    /// <summary>
    /// Contains default App
    /// Catches OnRender events
    /// </summary>
    public class AppMediator: Mediator
    {
        public const String SET_ROOTVISUAL_NOTIFICATION = "AppMediator_SetRootVisual";
        //public const String FPS_NOTIFICATION = "fps";
        
        public static String MEDIATORNAME = "SilverlightAppMediator";
        
        DateTime previousTime = DateTime.Now;
        //int ticks = 0;

        /// <summary>
        /// Leva com o App.xaml com Application
        /// </summary>
        /// <param name="viewComponent"></param>
        public AppMediator(Application viewComponent)
            : base(MEDIATORNAME, viewComponent)
        {
        }

        private new Application ViewComponent
        {
            get { return base.ViewComponent as Application; }
        }

        #region IMediator Members

        /// <summary>
        /// List the <c>INotification</c> names this <c>Mediator</c> is interested in being notified of
        /// </summary>
        /// <returns>The list of <c>INotification</c> names </returns>
        override public IList<string> ListNotificationInterests()
        {
            IList<String> notifications = base.ListNotificationInterests();
            notifications.Add(SET_ROOTVISUAL_NOTIFICATION);
            return notifications;
        }

        /// <summary>
        /// Handle <c>INotification</c>s
        /// </summary>
        /// <param name="notification">The <c>INotification</c> instance to handle</param>
        /// <remarks>
        ///     <para>
        ///        Typically this will be handled in a switch statement, with one 'case' entry per <c>INotification</c> the <c>Mediator</c> is interested in. 
        ///     </para>
        /// </remarks>
        override public void HandleNotification(INotification notification)
        {
            switch(notification.Name){
                case SET_ROOTVISUAL_NOTIFICATION:
                    SetRootVisual(notification.Body as FrameworkElement);
                    break;
            }
        }

        /// <summary>
        /// Called by the View when the Mediator is registered
        /// </summary>
        override public void OnRegister()
        {
            CompositionTarget.Rendering += new EventHandler(OnRender);
        }

        /// <summary>
        /// Called by the View when the Mediator is removed
        /// </summary>
        override public void OnRemove()
        {
        }

        #endregion

        private void SetRootVisual(FrameworkElement root)
        {
            ViewComponent.RootVisual = root;
        }
                
        private void OnRender(Object o, EventArgs args)
        {
            SendNotification(ApplicationFacade.TICK_COMMAND);
        }
    }
}
